import type { WhManagerBsTbRiskHazardsControlGetPageUsingPost } from "@/genapi/wh";
import { whManagerBsTbRiskHazardsControlGetPageUsingPost } from "@/genapi/wh";
import { usePage } from "@/hooks/use-page";

import type TeamDetails from "./components/team-details.vue";

export const [createState, injectState] = createInjectionState(
	() => {
		// 查询条件
		const filter =
			ref<WhManagerBsTbRiskHazardsControlGetPageUsingPost.Body["query"]>();

		const page = usePage({
			initPageSize: 15,
			fetch: async ({ pageSize, currentPage }) => {
				const { data } = await whManagerBsTbRiskHazardsControlGetPageUsingPost({
					data: {
						size: pageSize,
						current: currentPage,
						query: { ...filter.value },
					},
				});
				return {
					records: data?.records ?? [],
					total: data?.total ?? 0,
					pageSize,
				};
			},
		});

		watchDeep(filter, () => page.execute({ currentPage: 0 }));

		// 当前选中的列表项
		const selectedList = shallowRef<typeof page.records.value>([]);

		// 表格组件
		const tableRef =
			shallowRef<InstanceType<GlobalComponents["TableWidget"]>>();

		// 详情弹窗组件
		const teamDetailsRef = shallowRef<InstanceType<typeof TeamDetails>>();

		return {
			page,
			filter,
			selectedList,
			tableRef,
			teamDetailsRef,
		};
	},
	{
		injectionKey: "pageState",
	},
);
